﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TEAA;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    SystemManager sm = SystemManager.GetSystemManager(); //-

    protected void Page_Load(object sender, EventArgs e)
    {        
        Session["SelectedChanged"] = true;
        if (!IsPostBack)
        {
            fillddlDatabase();
            ddlDatabase.DataBind();
            setddlDatabaseSelectedItem();
            setInitalDB();
            fillViewDataTable();
        }
        else
        {
            fillViewDataTable();
        }
    }

    private void fillViewDataTable()
    {
        if (Session["page"].ToString() == "Data")
        {
            //(MainContent.Page as Data).fillddlTables();
            (MainContent.Page as Data).fillViewDataTable();
        }
    }

    /*public void setViewState(string db)
    {
        ViewState["activeDB"] = db;
    }

    public string getViewState()
    {
        return ViewState["activeDB"].ToString();
    }
    */
    public void setInitalDB()
    {
        Session["start"] = "yes";
        /*if (ddlDatabase.Items.Count > 0)
        {
            //Session["activDB"] = ddlDatabase.Items[0].ToString();
            sm.ActivateDatabase(ddlDatabase.Items[0].ToString());
        }
        else
        {
            Session["activDB"] = null;
        }*/
    }

    public void fillddlDatabase()
    {        
        List<string> dbs = new List<string>();
        dbs = sm.GetDatabases();        
        ddlDatabase.Items.Clear();
        foreach (string item in dbs)
        {
            ddlDatabase.Items.Add(new ListItem(item.ToString()));
        }
        //ddlDatabase.DataBind();        
        //setddlDatabaseSelectedItem();
    }

    public void setddlDatabaseSelectedItem()
    {
        string activDB = sm.GetActiveDatabase();
        if ((activDB == null) && (ddlDatabase.Items.Count > 0))
        {
            sm.ActivateDatabase(ddlDatabase.Items[0].ToString());            
        }
        ddlDatabase.ClearSelection();        
        ListItem li = ddlDatabase.Items.FindByValue(activDB);
        if (li != null) li.Selected = true;
    }

    public void ddlDatabase_SelectedChanged(object sender, EventArgs e)
    {        
        sm.ActivateDatabase(ddlDatabase.SelectedValue.ToString());//-        
        if ((sm.GetTables().Count > 0) && (Session["page"].ToString()=="FormatTable") )
            (MainContent.Page as FormatTable).fillDropDownListTables();

        if ((sm.GetTables().Count > 0) && (Session["page"].ToString() == "Data"))
        {
            (MainContent.Page as Data).fillddlTables();
            //(MainContent.Page as Data).fillViewDataTable();
        }
    }

    protected void btnDeleteDB_Click(object sender, EventArgs e)
    {        
        try
        {
            string database = sm.GetActiveDatabase();
            if (database == null)
            {
                database = ddlDatabase.Items[0].ToString();
                sm.ActivateDatabase(database);
            }
            sm.DropDatabase();
            fillddlDatabase();
            ddlDatabase_SelectedChanged(sender, e);
        }
        catch (Exception ex)
        {
            alertBox(ex);
        }
    }

    private void alertBox(Exception ex)
    {
        Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
        string errorMessage = "";
        Exception exception = ex;
        while (exception != null)
        {
            errorMessage += exception.Message + "\\n";
            exception = exception.InnerException;
        }
        Response.Write("alert('" + errorMessage + "');");
        Response.Write("</script>");
    }
}
